import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ken
 * Date: 2024-02-19
 * Time: 13:07
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public int nodeSize;
    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;
        return  A;
    }
    void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public List<Character> preorder (TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) {
            return null;
        }
        list.add(root.val);
        List<Character> leftTree = preorder(root.left);
        list.addAll(leftTree);
        List<Character> rightTree = preorder(root.right);
        list.addAll(rightTree);
        return list;
    }
    void inOrder (TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }
    int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
       return size2(root.left) + size2(root.right) + 1;
    }
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) +
                getLeafNodeCount(root.right);
    }
    int getKLevelNodeCount(TreeNode root, int k) {  //某层节点个数
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }
    TreeNode find(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode letf = find(root.left, val);
        if (letf != null) {
            return letf;
        }
        TreeNode right = find(root.right, val);
        if (right != null) {
            return right;
        }
        return null;
    }
    /*
    *第二个判断二叉树是否和第一棵树相同或者是第一棵树的子树
    */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(isSameTree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left, subRoot)) {
            return true;
        }
        if(isSubtree(root.right, subRoot)) {
            return true;
        }
        return false;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) { //两颗二叉树相不相同
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        return isSameTree(p.left, q.left) &&
                isSameTree(p.right, q.right);
    }
    public TreeNode invertTree(TreeNode root) {  //翻转二叉树
        if(root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
